import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';


class SingleDiary extends StatefulWidget {

  String token;
  String time;
  String content;
  String newContent;
  int id;
  SingleDiary(this.token,this.time,this.content,this.id);
  @override
  _SingleDiary createState() => new _SingleDiary();
}

class _SingleDiary extends State<SingleDiary> {

  Response response;
  int code;


  //修改日记提示框
  _tips(){
    showDialog(
        context: context,
        builder:(context){
          return AlertDialog(
            title: Text("提示"),
            content: Text("真的要修改这篇日记吗？"),
            actions: [
              FlatButton(
                child: Text("取消",style: TextStyle(color: Colors.black),),
                color: Colors.greenAccent[100],
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(30)
                ),
                onPressed: (){
                  Navigator.pop(context);
                },),
              FlatButton(
                child: Text("确定",style: TextStyle(color: Colors.black),),
                color: Colors.greenAccent[100],
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(30)
                ),
                onPressed: (){
                  Navigator.pop(context);
                  _updateDiary();
                },)
            ],
            backgroundColor:Colors.lightGreen[50],
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10)
            ),
          );
        }
    );
  }

  //修改日记
  _updateDiary(){
    Dio().post("http://121.196.101.7:8080/treehole/diary/updateDiary",
        options:Options(headers: {"Token":widget.token},),queryParameters: {"diaryId":widget.id,
          "title":widget.time,"content":widget.newContent} ).then((value) {
      response=value;
      print(response.data);
      code=response.data["code"];
      if(code==0){
        setState(() {
          Fluttertoast.showToast(
            msg: "修改日记成功",
            backgroundColor: Colors.amber[50],
            textColor: Colors.black,
            fontSize: 15,
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,);
          Navigator.pop(context,'pop');
        });
      }else{
        Fluttertoast.showToast(
          msg: "修改失败，请重试",
          backgroundColor: Colors.amber[50],
          textColor: Colors.black,
          fontSize: 15,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,);
      }
    });
  }


  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text('某刻',style:TextStyle(color: Colors.black)),
        backgroundColor: Colors.white70,
        leading: IconButton(
          icon:Icon(Icons.arrow_back,color: Colors.black),
          onPressed: () {Navigator.pop(context);},
        ),
      ),
      backgroundColor: Colors.amber[50],
      body: Container(
        decoration: BoxDecoration(
            image: DecorationImage(
                fit: BoxFit.cover,
                image: AssetImage("images/paperpage1.jpg")//获取信纸
            )
        ),
        child: Column(
          children: [
            Expanded(
                flex:1,
                child: SizedBox()),
            Expanded(
              flex: 2,
              child: Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: Container(
                      margin: EdgeInsets.only(left: 10),
                      alignment: Alignment.centerLeft,
                      child: Text("亲爱的自己：",style: TextStyle(
                          fontSize: 16,
                          color: Colors.black
                      ),),
                    ),
                  ),
                  Expanded(
                      flex: 1,
                      child: Container(
                        alignment: Alignment.centerRight,
                        child: IconButton(
                          icon: Icon(Icons.email_rounded,color: Colors.black,),
                          onPressed: (){
                           _tips();
                          },
                        ),
                      )
                  ),
                ],
              ),
            ),
            Expanded(
                flex: 19,
                child: Padding(
                  padding: EdgeInsets.all(10),
                  child: TextField(
                    maxLines: 15,
                    decoration: InputDecoration(
                      border: OutlineInputBorder(
                          borderSide: BorderSide(
                              color: Colors.transparent
                          )
                      ),
                      contentPadding:EdgeInsets.all(10),
                      isCollapsed: true,
                    ),
                    controller: TextEditingController()..text=widget.content,
                    onChanged: (value){
                      widget.newContent=value;
                    },
                  ),
                )),
            Expanded(
                flex: 4,
                child: Container(
                  alignment: Alignment.bottomRight,
                  margin: EdgeInsets.only(right: 10),
                  child: Column(
                    children: [
                      Expanded(
                          flex: 2,
                          child: Text("那天的小可爱",textAlign: TextAlign.center,)),
                      Expanded(
                          flex: 1,
                          child: SizedBox()),
                      Expanded(
                          flex: 2,
                          child: Text(widget.time)
                      )
                    ],
                  ),
                )),
            Expanded(
                flex: 1,
                child: SizedBox())
          ],
        ),
      ),
    );
  }



}